package helpers 
{
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import objects.BtnNext;
	import objects.BtnPrev;
	import objects.HUD;
	import objects.Squere;
	
	/**
	 * ...
	 * @author Ja
	 */
	public class Screen extends Sprite 
	{
		
		private var _stage:Stage
		
		public var SCREEN_WIDTH:int;
		public var SCREEN_HEIGHT:int;
		
		private var _inputController:InputController;
		
		private var sq:Squere;
		
		private var _hud:HUD;
		private var _btnNext:BtnNext;
		private var _btnPrev:BtnPrev;
		
		private var _onNext:Function = null;
		private var _onPrev:Function = null;
		
		public function Screen(stage:Stage, onNext:Function=null,onPrev:Function=null) 
		{
			_onNext = onNext;
			_onPrev = onPrev;
			this.name = "Screen";
			_stage = stage;
			SCREEN_WIDTH = _stage.stageWidth;
			SCREEN_HEIGHT = _stage.stageHeight;
			addContent();
			addSignals();
			addHUD();
		}
		
		private function addHUD():void 
		{
			_hud = new HUD();
			addChild(_hud);
			_hud.mouseEnabled = false;
			addButtons();
		}
		
		private function addContent():void 
		{
			sq = new Squere(SCREEN_WIDTH, SCREEN_HEIGHT, 0x4992CD,false);
			addChild(sq);
			
		}
		
		private function addSignals():void 
		{
			
			_inputController = InputController.getInstance(MouseEvent.CLICK, MouseEvent);
			
			_inputController.addSignal(this, clicked,MouseEvent.CLICK,MouseEvent);
			
			
			
		}
		private function up(e:MouseEvent):void 
		{
			trace("up")
		}
		private function down(e:MouseEvent):void 
		{
			trace("down")
		}
		
		private function clickedNext(target:DisplayObject=null):void 
		{
			trace("clickedNext")
			if (target)
			{
				TweenLite.to(target, 2, { x: -target.width - 20 } );
			}
			if (_onNext != null) _onNext();
			
		}
		private function clickedPrev(target:DisplayObject=null):void 
		{
			trace("clickedPrev")
			if (target)
			{
				TweenLite.to(target,2,{x:0});
			}
			if (_onPrev != null) _onPrev();
		}
		
		
		private function clicked(e:MouseEvent):void 
		{
			e.stopPropagation();
			//trace(e.currentTarget)
			switch (e.target) 
			{
				case _btnNext:
					clickedNext(sq);
				break;
				case _btnPrev:
					clickedPrev(sq);
				break;
				default:trace("clicked nothing");
			}
		}
		
		private function addButtons():void 
		{
			
			
			_btnNext = new BtnNext();
			_btnNext.y = SCREEN_HEIGHT - int(_btnNext.height * 1.5);
			_btnNext.x = (SCREEN_WIDTH>>1)+int(_btnNext.width*.2);
			addChild(_btnNext);
			
			_btnPrev = new BtnPrev();
			_btnPrev.y = SCREEN_HEIGHT - int(_btnPrev.height*1.2);
			_btnPrev.x = (SCREEN_WIDTH>>1)-_btnPrev.width;
			addChild(_btnPrev);
			
		}
		
	}

}